//
//  BasicPhotoTableViewCell.m
//  CarTown
//
//  Created by xionghao on 15/8/6.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "BasicPhotoTableViewCell.h"
#import <UIImageView+WebCache.h>
#import "SDPhotoBrowser.h"

@interface BasicPhotoTableViewCell ()<SDPhotoBrowserDelegate>
@property (nonatomic,strong) NSMutableArray *Marray;
@end

@implementation BasicPhotoTableViewCell

+(instancetype)cellWithTableView:(UITableView *)tablview{
    static NSString *phone=@"photo";
    
    BasicPhotoTableViewCell *cell=[tablview dequeueReusableCellWithIdentifier:phone];
    if (cell==nil) {
        [tablview registerNib:[UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil] forCellReuseIdentifier:phone];
        cell=[tablview dequeueReusableCellWithIdentifier:phone];
    }
    
    
    
    return cell;
}
- (void)awakeFromNib {
    // Initialization code
    UITapGestureRecognizer *tap1=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imgclick:)];
    [self.photo1 addGestureRecognizer:tap1];
    UITapGestureRecognizer *tap2=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imgclick:)];
    [self.photo2 addGestureRecognizer:tap2];
    UITapGestureRecognizer *tap3=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imgclick:)];
    [self.photo3 addGestureRecognizer:tap3];
    UITapGestureRecognizer *tap4=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imgclick:)];
    [self.photo4 addGestureRecognizer:tap4];
}
-(NSMutableArray *)Marray{
    if (_Marray==nil) {
        _Marray=[NSMutableArray array];
        
    }
    return _Marray;
}
-(void)setImgarray:(NSArray *)imgarray{
    _imgarray=imgarray;
    [self.Marray removeAllObjects];
    for (int i=0; i<_imgarray.count; i++) {
        NSString *imgstr=_imgarray[i];
        BOOL isnull=[imgstr isEqualToString:@""];
        switch (i) {
            case 0:
                if (!isnull) {
                    [self.photo1 sd_setImageWithURL:[NSURL URLWithString:[NSString getImageURLstr:imgstr]] placeholderImage:[UIImage imageNamed:@"placehoder"]];
                    [self.Marray addObject:self.photo1];
                }else{
                    self.photo1.hidden=YES;
                }
                break;
            case 1:
                if (!isnull) {
                    [self.photo2 sd_setImageWithURL:[NSURL URLWithString:[NSString getImageURLstr:imgstr]] placeholderImage:[UIImage imageNamed:@"placehoder"]];
                     [self.Marray addObject:self.photo2];
                }else{
                    self.photo2.hidden=YES;
                }
                break;
            case 2:
                if (!isnull) {
                    [self.photo3 sd_setImageWithURL:[NSURL URLWithString:[NSString getImageURLstr:imgstr]] placeholderImage:[UIImage imageNamed:@"placehoder"]];
                     [self.Marray addObject:self.photo3];
                }else{
                    self.photo3.hidden=YES;
                }
                break;
            case 3:
                if (!isnull) {
                    [self.photo4 sd_setImageWithURL:[NSURL URLWithString:[NSString getImageURLstr:imgstr]] placeholderImage:[UIImage imageNamed:@"placehoder"]];
                     [self.Marray addObject:self.photo4];
                }else{
                    self.photo4.hidden=YES;
                }
                break;
            default:
                break;
        }
        
    }
    
}

-(UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index{
    UIImageView *imageview=self.Marray[index];
    return imageview.image;
}

-(void)imgclick:(UITapGestureRecognizer *)tap{
    NSLog(@"click");
    //    NSLog(@"btntag%d",btn.tag);
    
    UIImageView *imageview=tap.view;
    SDPhotoBrowser *browser=[[SDPhotoBrowser alloc]init];
    browser.sourceImagesContainerView = self.contentView; // 原图的父控件
    browser.imageCount = self.Marray.count; // 图片总数
    browser.currentImageIndex = [self.Marray indexOfObject:imageview];
    browser.delegate = self;
    [browser show];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
